// pages/myJifen/myJifen.js
const app = getApp()
var url = require('../../utils/url.js');
var token;
var page = 1;
var last_page = 0;
var loadMore = function (that) {
  wx.showLoading({
    title: '加载中',
  });
  if (page == 1) {
    page = 2
  }
  if (page < last_page || page == last_page) {
    console.log(page)
    wx.request({
      url: url.URl + 'my/jifen/list',
      data: {},
      method: 'get',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res.data)
        wx.hideLoading();
        that.setData({
          list: that.data.list.concat(res.data.data.data)
        });
        page++;
      }
    });
  } else {
    wx.hideLoading();
    that.setData({
      nodata: 1,
      skill: true
    })
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
     jifen:"",
     list:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
     wx.getStorage({
       key: 'token',
       success: function(res) {
         token=res.data;
         that.getJifen()
         that.getJifenList()
       },
     })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getJifen(){
    var that=this;
    wx.request({
      url: url.URl + 'my/jifen',
      data: {},
      method: 'get',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res)
        var msg = res.data.msg
        if (res.data.code == 200) {
           that.setData({
             jifen:res.data.data
           })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getJifenList() {
    var that = this;
    if (page > 1) {
      page--;
    }
    wx.request({
      url: url.URl + 'my/jifen/list',
      data: {},
      method: 'get',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res)
        var msg = res.data.msg
        if (res.data.code == 200) {
          last_page = res.data.data.last_page
          that.setData({
            list: res.data.data.data
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    page=1;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
     
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(page)
    var that = this;
    if (that.data.index != 0) {
      loadMore(that)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})